package com.nbst.comnutil;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.metadata.Table;
import com.alibaba.excel.support.ExcelTypeEnum;

public class ExcelDataUtil {
	public static void dataExportExcel(HttpServletResponse response, String fileName, List<List<String>> headlist,
			List<List<String>> datalist) {
		try {
			fileName = new String((fileName).getBytes(), "UTF-8");
			OutputStream out = response.getOutputStream();
			ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX);
			Sheet sheet1 = new Sheet(1, 0);
			sheet1.setAutoWidth(Boolean.TRUE);
			Table table = new Table(1);
			table.setHead(headlist);
			sheet1.setSheetName("sheet1");
			writer.write0(datalist, sheet1, table);
			response.setCharacterEncoding("utf-8");
			// response.setContentType("application/vnd.ms-excel");
			// response.setHeader("content-Disposition","attachment;filename=" + fileName);
			// response.setHeader("content-Disposition",
			// "attachment;filename=" + URLEncoder.encode(fileName + ".xlsx", "utf-8"));

			response.setContentType("application/x-msdownload");
			response.setHeader("Content-Disposition",
					"attachment; filename=" + URLEncoder.encode(fileName + ".xlsx", "UTF-8"));

			out.flush();
			writer.finish();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
